package dome_009;

import java.util.concurrent.TimeUnit;

/**
 * <p>Description: [ reentrantlock用于替代synchronized
 * 本例中由于test1锁定this，只有test1执行完毕的时候,test2才能执行]</p>
 * Created on 2020/12/11
 *
 * @author <a href="mailto: 819712047@qq.com">谢振东 </a>
 * @version 1.0
 */
public class ReentrantLock1 {
    public synchronized void test1() {
        for (int i = 0; i < 10; i++) {
            System.out.println("execute test1(" + i + ")........");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void test2() {
        System.out.println("execute test2........");
    }

    public static void main(String[] args) {
        ReentrantLock1 rl = new ReentrantLock1();
        new Thread(rl::test1).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(rl::test2).start();
    }
}
